package apii.servlet.prof;

import java.io.*;
import java.sql.*;

import javax.servlet.http.*;

import apii.server.*;
import apii.servlet.ServletUtils;

public class ProfUtils {
	static final String URL_BASE = "/APII";
	static final String PROF_HOME = "/APII/prof";
	public static final String PROF = "/prof";

	public static void main(String[] args) throws IOException {
		try (FileInputStream fis = new FileInputStream(
				"WebContent\\html\\ProfSubjects.html");
				InputStreamReader isr = new InputStreamReader(fis, "utf-8");
				BufferedReader br = new BufferedReader(isr);) {
			System.out
					.println("response.setContentType(\"text/html; charset=utf-8\");");
			System.out.println("PrintWriter pw = response.getWriter();");
			String s;
			while ((s = br.readLine()) != null) {
				System.out.print("pw.println(\"");
				System.out.print(s.replace("\\", "\\\\").replace("\"", "\\\""));
				System.out.println("\");");
			}
		} 
	}

	public static void startResponse(HttpServletResponse response,
			String title, ProfData profData) throws IOException {
		response.setContentType("text/html; charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
		pw.println("<html>");
		pw.println("<head>");
		pw.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
		pw.println("<title>" + title + "</title>");
		pw.println("</head>");
		pw.println("<body>");
		pw.println("	<hr>");
		pw.println("	<table bgcolor=\"#8DB8B6\" width=\"100%\">");
		pw.println("		<tr>");
		pw.println("			<td align=\"center\"><a href=\"" + PROF_HOME
				+ "\">Главная страница</a> | <a href=\"" + PROF_HOME
				+ "/subjects\">Мои");
		pw.println("					предметы</a> | <a href=\"" + PROF_HOME
				+ "/users\">Учётные записи</a> | <a href=\"" + PROF_HOME
				+ "/public-code\">Общественный");
		pw.println("					код</a></td>");
		pw.println("");
		pw.println("			<td align=\"right\">" + profData.getSurname() + " "
				+ profData.getName() + " " + profData.getPatronimic()
				+ " | <a href=\"" + URL_BASE + "/logout\">Log");
		pw.println("					out</a>");
		pw.println("			</td>");
		pw.println("		</tr>");
		pw.println("	</table>");
		pw.println("	<hr>");
	}

	public static void homeResponse(HttpServletResponse response)
			throws IOException {
		PrintWriter pw = response.getWriter();
		pw.println("	<H1>Выберите один из вариантов, что бы начать работу.</H1>");
		pw.println("	<ul>");
		pw.println("		<li><a href=\"" + PROF_HOME + "/subjects\">Мои");
		pw.println("					предметы</a></li>");
		pw.println("		<li><a href=\"" + PROF_HOME
				+ "/users\">Учётные записи</a></li>");
		pw.println("		<li><a href=\"" + PROF_HOME
				+ "/public-code\">Общественный");
		pw.println("					код</a></li>");
		pw.println("	</ul>");
	}

	public static void subjectsResponse(Connection c,
			HttpServletResponse response, int userId) throws SQLException,
			IOException {
		try (PreparedStatement st = c
				.prepareStatement("select discipline_id, name from disciplines");) {

			PrintWriter pw = response.getWriter();
			pw.println("	<H1>Выберите предмет.</H1>");
			pw.println("	<ul>");
			pw.println("		<li><a href=\"\">Предмет1</a></li>");
			pw.println("		<li><a href=\"\">Предмет2</a></li>");
			pw.println("	</ul>");
		}
	}

	public static final void subjectsResponseJenyaDobabilNadoRefaktor(
			Connection c, HttpServletResponse response, int userId)
			throws SQLException, IOException {
		try (PreparedStatement st = c
				.prepareStatement("select discipline_id, name from disciplines "
						+ "where disciplines.owner_id = ?");) {
			st.setInt(1, userId);
			ResultSet rs = st.executeQuery();

			PrintWriter pw = response.getWriter();
			pw.println("	<H1>Выберите предмет.</H1>");
			pw.println("	<ul>");
			while (rs.next()) {
				pw.println("		<li><a href=\"" + ServletUtils.PREFIX
						+ "/prof/subjects/"
						+ ServletUtils.getDisciplineURLElement(rs.getInt(1))
						+ "\">" + rs.getString(2) + "</a></li>");
			}
			pw.println("	</ul>");
		}
	}

	public static void finishResponse(HttpServletResponse response)
			throws IOException {
		PrintWriter pw = response.getWriter();
		pw.println("</body>");
		pw.println("</html>");
	}

	public static ProfData getSessionProf(Connection c,
			HttpServletRequest request) throws SQLException {
		HttpSession session = request.getSession(false);
		if (session == null)
			return null;
		try (PreparedStatement st = c
				.prepareStatement("select users.user_id, name, surname, patronimic from user_session_data join users on user_session_data.user_id = users.user_id where session_id = ? and logged_in");) {
			st.setString(1, session.getId());
			ResultSet rs = st.executeQuery();
			if (!rs.next())
				return null;
			return new ProfData(rs.getInt(1), rs.getString(2), rs.getString(3),
					rs.getString(4));
		}
	}

	public static void redirectToLogin(HttpServletResponse response) {
		response.setStatus(HttpServletResponse.SC_SEE_OTHER);
		response.setHeader("Location", ProfUtils.URL_BASE + "/login");
	}

	public static void redirectToException(HttpServletResponse response) {
		try {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		} catch (IllegalArgumentException | IOException e) {
			e.printStackTrace();
		}
	}
}
